#include"../include/lib_headers.h"
char* remove_file(char *path) {
	int i = 0, j = 0, k = 0, match = 0, t = 0;
	char s[SIZE] = "", *split_char, *p[SIZE], copy[SIZE], *str;
	node *temp, *prev, *deleted;
	
	/* VFS not mounted */
	if(h == NULL || l1==NULL ){
		sprintf(output_message,"%s %s","removefile_FAILURE",ERR_VFS_REMOVEFILE_02);	
		return output_message;
	}
	
	/* Insufficient Arguments */
	if(strlen(path) == 0){
		sprintf(output_message,"%s %s","removefile_FAILURE",ERR_VFS_REMOVEFILE_00);	
		return output_message;
	}
	
	/* File not found */
	if(find_node(l1->next,path)==NULL || path[0] != '/' ){
		sprintf(output_message,"%s %s","removefile_FAILURE",ERR_VFS_REMOVEFILE_01);	
		return output_message;
	}
	
	/* Cannot delete a directory using this operation */
	if(strcmp(find_node(l1->next,path)->fd_pointer->fileType, "dir") == 0){
		sprintf(output_message,"%s %s","removefile_FAILURE",ERR_VFS_REMOVEFILE_01);	
		return output_message;
	}

	split_char = "/";
	str = add_slash(path);
	strcpy(s, str);
	i = splits_directory(s, p, split_char);
	temp = l1->next;
	prev = temp;
	match = 0;
	j = 0;
	while (j < i) {
		while (temp != NULL) {
			if (strcmp(temp->fd_pointer->fileName, p[j]) == 0) {
				match = 1;
				j++;
				if (j == i) {
					break;
				}
				prev = temp;
				temp = temp->child;
			}
			if (match == 0) {
				prev = temp;
				temp = temp->sibling;
			} else if (match == 1) {
				match = 0;
				continue;
			}
		}
	}
	if (prev->child == temp) {
		/* printf("if\n"); */
		prev->child = temp->sibling;
		del(temp->fd_pointer->fullPath);
		htable_delete(hashtable,*(temp->fd_pointer));
		freelist[temp->fd_pointer->blockno]=0;
		h->usedFds--;
		free(temp);
	} else if (prev->sibling == temp) {
		/* printf("else if\n"); */
		prev->sibling = prev->sibling->sibling;
		del(temp->fd_pointer->fullPath);		
		htable_delete(hashtable,*(temp->fd_pointer));
		freelist[temp->fd_pointer->blockno]=0;
		h->usedFds--;
		free(temp);
	} else if (l1->next == prev) {
		/* printf("else\n"); */
		l1->next = prev->sibling;
		del(prev->fd_pointer->fullPath);		
		htable_delete(hashtable,*(prev->fd_pointer));
		freelist[prev->fd_pointer->blockno]=0;
		h->usedFds--;
		free(prev);
	}
	sprintf(output_message,"%s","removefile_SUCCESS");	
	return output_message;
}
